<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * Class UserRole
 * @package App\Models
 * @author zjw
 */
class UserRole extends Model{

    const FIXED_MEAL_MARK = 1;          // 固定餐标
    const CHARGING_MEAL_MARK = 2;       // 计费餐标
    const ADDITION_MEAL_MARK = 3;       // 计全额

    const CAN_ORDER = 1;                // 能订餐
    const CAN_NOT_ORDER = 0;            // 不能订餐

    const IS_DEFAULT = 1;               //默认角色
    const NOT_DEFAULT = 0;               //不是默认角色

    /**
     * @param $query
     * @return mixed
     */
    protected $fillable = [
      'name', 'school_id', 'description', 'can_breakfast', 'can_lunch', 'can_dinner', 'breakfast_rate', 'lunch_rate', 'dinner_rate', 'morning_price_rule', 'morning_price', 'lunch_price_rule', 'lunch_price', 'dinner_price_rule', 'dinner_price'
    ];

    protected $casts = [
        'breakfast_rate' => 'float',
        'lunch_rate' => 'float',
        'dinner_rate' => 'float',
        'morning_price' => 'float',
        'lunch_price' => 'float',
        'dinner_price' => 'float'
    ];

    /**
     * @param $query
     * @param $name
     */
    public function scopeNameLike($query, $name)
    {
        $query->when($name, function($query) use ($name) {
            $query->where('name', 'like', $name . '%');
        });
    }

    /**
     * @param $query
     * @param $schoolId
     */
    public function scopeSchoolIs($query, $schoolId)
    {
        $query->when($schoolId, function($query) use ($schoolId) {
            $query->where("school_id", $schoolId);
        });
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function school() {
        return $this->belongsTo(School::class, 'school_id');
    }

    /**
     * @param $query
     * @return mixed
     */
    public function scopeIsSchool($query){
        return $query->where("school_id",\Auth::user()->school_id);
    }

    /**
     * @return bool
     * 是否能订早餐
     */
    public function canBookBreakfast(){
        return $this->can_breakfast == self::CAN_ORDER;
    }

    /**
     * @return bool
     * 是否能订午餐
     */
    public function canBookLunch(){
        return $this->can_lunch == self::CAN_ORDER;
    }

    /**
     * @return bool
     * 是否能定晚餐
     */
    public function canBookDinner(){
        return $this->can_dinner == self::CAN_ORDER;
    }

    /**
     * @param $query
     * @return mixed
     */
    public function scopeIsDefault($query){
        return $query->where('is_default', self::IS_DEFAULT);
    }

    /**
     * @return array
     * 获取可定餐的类型集合
     */
    public function getCanBookTypesAttribute(){
        $types = [];
        if($this->canBookBreakfast()) {
            $types[] = BookRule::EAT_TYPE_BREAKFAST;
        }
        if($this->canBookLunch()) {
            $types[] = BookRule::EAT_TYPE_LUNCH;
        }
        if($this->canBookDinner()) {
            $types[] = BookRule::EAT_TYPE_DINNER;
        }
        return $types;
    }
}
